import { Component, OnInit,Input,EventEmitter,Output } from '@angular/core';
import {Service} from '../../server/server';
import {HttpClient} from '@angular/common/http';
import {CudTableService} from '../../public/serve/cud-table/cud-table.service';
import {Pagination} from '../../public/serve/pcrudInfo';
import {RemoveTableService} from '../../public/serve/remove-table/remove-table.service';
import {keyword} from '../../keyword/keyword.data';
declare var $:any;

@Component({
    selector: 'app-internal-indexed-modal-table',
    templateUrl: './internal-indexed-modal-table.component.html',
    styleUrls: ['./internal-indexed-modal-table.component.css']
})
export class InternalIndexedModalTableComponent implements OnInit {
    @Input() internalModalTable:any = '';
    @Input() internalParam : any;
    isSelectAll : boolean = false;
    isChose : boolean = false;
    public tabVal: any;
    @Input() paramData: any;
    @Input() searchDataMore: any;
    public totalItems: number;
    public delete :any;
    public internalindexeddeleteentEmit: any;
    relThemeDetail : object;
    constructor(public removeTableService: RemoveTableService, private server: Service,private http: HttpClient, public request: CudTableService,) { }
    @Output()
    relThemeDetailOutput: EventEmitter<any> = new EventEmitter();
    public internalModalMore: boolean = false;
    public internalThemeWordsTablesRelEmit: any;
    public internalThemeWordsLeftEmit: any;
    currentThemeWordInfo : object;
    currentType = 1;
    ngOnInit() {
        // this.getIndexedTables(
        //     this.pageNumber,
        //     this.pageSize,
        //     this.paramData,
        //     this.searchDataMore
        // );

        this.internalindexeddeleteentEmit = this.server.internalindexeddeleteentEmit.subscribe((value: any) => {
            if (value) {
                this.getIndexedTables(
                    this.pageNumber,
                    this.pageSize,
                    this.paramData,
                    this.searchDataMore
                );
            }
        });

        this.internalThemeWordsLeftEmit = this.server.internalThemeWordsLeftEmit.subscribe((value: any) => {
            if (value) {
                this.currentThemeWordInfo = value
            }
        });
        this.internalThemeWordsTablesRelEmit = this.server.internalThemeWordsTablesRelEmit.subscribe((value: any) => {
            if (value == 2) {
                //关联主题词查询
                this.currentType = 2;
                this._getRelThemeList(
                    this.pageNumber,
                    this.pageSize,
                    this.paramData,
                    this.searchDataMore);
            }else{
                this.currentType = 1;
                this.getIndexedTables(
                    this.pageNumber,
                    this.pageSize,
                    this.paramData,
                    this.searchDataMore
                );
            }
        });
    }


    ngOnDestroy() {
        this.internalThemeWordsTablesRelEmit ? this.internalThemeWordsTablesRelEmit.unsubscribe() : '';
        this.internalThemeWordsLeftEmit ? this.internalThemeWordsLeftEmit.unsubscribe() : '';
        this.internalindexeddeleteentEmit ? this.internalindexeddeleteentEmit.unsubscribe() : '';
    }

    /**
     * 查询标引主题词列表
     */
    public getIndexedTables(pageNumber, pageSize,paramData, queryCriteria){
        let url = this.server.baseUrl + 'Service/IdxProduct/getProductThemeWordsList';
        let data = {
            "token": "b25dbd7138cf48e09941087e6d9a2e93",
            // "checkStatus":1,
            "productId":this.internalParam.productId,
            "taskId":this.internalParam.taskId,
            "themeTableId":this.internalParam.themeTableId,
            "id":'',
            "pageNo":pageNumber,
            "pageSize":pageSize,
            "startTime":"",
            "endTime":"",
            "themeWordsName":queryCriteria
        };
        this.request.postRequest(url,data).then((res:any) => {
            if (res.result_code === '0000') {
                this.tabVal = res.result;
                this.totalItems = res.length;
            }else{
                // this.hintsService._operationHints(res.message,res.result_code);
            }
            // this._cancelChecked();
            return;
        })
            .catch((err: any)=>{
                // this.hintsService._operationHints("error","error");
                // this._cancelChecked();
                return;
            });
    }

    //根据标引主题词和表获取已关联主题词列表
    _getRelThemeList(pageNumber, pageSize,paramData, queryCriteria){

        if(this.currentThemeWordInfo){
            let data = {
                // pageNo        : this.pageNumber,
                // pageSize      : this.pageSize,
                pageNo        : pageNumber,
                pageSize      : pageSize,
                themeWordsName : queryCriteria,
                themeTableId  :  this.currentThemeWordInfo['themeTableId'], // 左侧主题词表id
                refThemeTableId  :  this.internalParam['themeTableId'], // 当前主题词表id
                themeWordsId  :  this.currentThemeWordInfo['themeWordsId'],    //左侧主题词id
                productId  : this.internalParam['productId'],
                taskId  : this.internalParam['taskId']
            };
            let url = this.server.baseUrl +  'Service/IdxProduct/getProductThemeRelationList';
            this.request.postRequest(url,data).then((res:any) => {
                if (res.result_code === '0000') {
                    this.tabVal = res.result;
                    this.totalItems = res.length;
                }else{
                    // this.hintsService.messages(res.message,res.result_code);
                }
                return;
            })
                .catch((err: any)=>{
                    // this.hintsService.messages("error","error");
                    return;
                });
        }
    }


    // 表格
    tableNumber: number = 0;
    // 当前页（默认为第一页）
    public pageNumber: number = 1;

    // 每页显示多少条数据（默认每页显示20条）
    public pageSize: number = 20;
    // id
    public id: string = '0';
    // 分页
    public pagination: Pagination;
    isLoadTableFromPaginationHandler(pagination: Pagination) {
        this.pagination = pagination;

        this.pageNumber = this.pagination[0].pageNumber;
        this.pageSize = this.pagination[0].pageSize;
        this.id = this.pagination[0].id;

        this.tableNumber = (pagination[0].pageNumber - 1) * pagination[0].pageSize;

        if( this.currentType == 2){
            this._getRelThemeList(
                this.pageNumber,
                this.pageSize,
                this.paramData,
                this.searchDataMore);
        }else{
            this.getIndexedTables(
                this.pageNumber,
                this.pageSize,
                this.paramData,
                this.searchDataMore
            );
        }
    }

    /**
     * 删除（弹窗）
     * @param info
     */
    _delete(id,theme_id) {
        this.server.internalDeleteThemeIdEmit.emit(theme_id);
        this.removeTableService.keywordRemoveChose(true,id,1);
        $("#delete").modal("show");
    }

    /**
     * 删除（弹窗）
     * @param info
     */
    _deleteRel(id) {
        this.removeTableService.keywordRemoveChose(true,id,1);
        $("#reltheme-delete").modal("show");
    }

    /**
     * 全选
     * @param bool
     * @param keywordTabVal
     */
    _selectAll(bool,keywordTabVal){
        this.isSelectAll = this.isChose = this.removeTableService.keywordRemoveAll(bool, keywordTabVal, 'id');
        if (this.isSelectAll == false) {
            if( this.currentType == 2){
                this._getRelThemeList(
                    this.pageNumber,
                    this.pageSize,
                    this.paramData,
                    this.searchDataMore);
            }else{
                this.getIndexedTables(
                    this.pageNumber,
                    this.pageSize,
                    this.paramData,
                    this.searchDataMore
                );
            }
        }
    }

    /**
     * 单选
     * @param bool
     * @param themeWordsId
     * @param length
     */
    _chose(bool, id, length){
        this.isSelectAll = this.removeTableService.keywordRemoveChose(bool, id, length)['bool'];
    }

    /**
     * 重置表格复选框、选中的ID
     */
    private _cancelChecked(){
        this.isChose = this.isSelectAll = false;
        this.removeTableService._clearId("id");
    }

    //查询
    searchMore() {
        if( this.currentType == 2){
            this._getRelThemeList(
                this.pageNumber,
                this.pageSize,
                this.paramData,
                this.searchDataMore);
        }else{
            this.getIndexedTables(
                this.pageNumber,
                this.pageSize,
                this.paramData,
                this.searchDataMore
            );
        }
    }
    //响应搜索
    changesearch() {
        if( this.currentType == 2){
            this._getRelThemeList(
                this.pageNumber,
                this.pageSize,
                this.paramData,
                this.searchDataMore);
        }else{
            this.getIndexedTables(
                this.pageNumber,
                this.pageSize,
                this.paramData,
                this.searchDataMore
            );
        }
    }

    moreList(){
        this.pageNumber = 1;
        this.pageSize = 20;
        if( this.currentType == 2){
            this._getRelThemeList(
                this.pageNumber,
                this.pageSize,
                this.paramData,
                this.searchDataMore);
        }else{
            this.getIndexedTables(
                this.pageNumber,
                this.pageSize,
                this.paramData,
                this.searchDataMore
            );
        }
    }
}
